/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.poker.zone;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.managers.IZoneManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Hung
 */
public class LoginEventHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        String username = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
        String password = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);

        ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
        try {
            Connection conn = getParentExtension().getParentZone().getDBManager().getConnection();

            PreparedStatement sql = conn.prepareStatement("SELECT password FROM users WHERE username = ?");
            sql.setString(1, username);

            ResultSet result = sql.executeQuery();
            SFSArray row = SFSArray.newFromResultSet(result);

            if (row.size() > 0) {
                if (!getApi().checkSecurePassword(session, row.getSFSObject(0).getUtfString("password"), password)) {
                    SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
                    data.addParameter(username);
                    throw new SFSLoginException("Login failed for user: " + username, data);
                }

                IZoneManager zoneManager = getParentExtension().getParentZone().getZoneManager();
                for (int i = 0; i < zoneManager.getZoneList().size(); i++) {
                    User user = zoneManager.getZoneList().get(i).getUserByName(username);
                    if (user != null) {
                        getApi().kickUser(user, null, "Có người vừa đăng nhập tài khoản của bạn!", 10);
                        break;
                    }
                }
            } else {
                SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
                data.addParameter(username);
                throw new SFSLoginException("Login failed for user: " + username, data);
            }

            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            trace(ExtensionLogLevel.WARN, " SQL Failed: " + e.toString());
        }
    }
}
